//
//  SupplierManager.m
//  takeout
//
//  Created by Derekc on 12-2-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SupplierManager.h"
#import "ResolveXML.h"
#import "Constants.h"

@implementation SupplierManager
@synthesize tempSupplier;
@synthesize result;
-(NSArray *)getAllSupplierByCategoryId:(NSString *)suppierCategoryId{
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_SUPPLIERS_BY_CATEGORIES,[NSString stringWithFormat:@"?supCateId=%@",suppierCategoryId]]];
    return [self getSupplierByUrl:URIString];
}
-(NSArray *)getAllSupplier{
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN,SERVICE_ALL_SUPPLIERS];

    return [self getSupplierByUrl:URIString];
}


-(Supplier *)getSupplierDetailById:(NSString *) supId{
    __strong NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_SEARCH_SUPPLIER_BY_ID, supId]];
    __strong NSURL *url2 = [[NSURL alloc] initWithString:[URIString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    __strong NSMutableURLRequest* request = [NSMutableURLRequest new];    
    [request setURL:url2];    
    [request setHTTPMethod:@"GET"]; 
    __strong NSHTTPURLResponse* response;    
    __strong NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
    __strong NSString* responseXMLResult=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    __strong NSError *error;
    __strong GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult options:0 error:&error];
    
    
    
    __strong NSArray *children = [doc.rootElement children]; 
    tempSupplier = [[Supplier alloc] init];
    
    tempSupplier.supplierId = [[[(GDataXMLElement*)[children objectAtIndex:0] childAtIndex:0] stringValue] intValue];
    tempSupplier.supplierName = [[(GDataXMLElement*)[children objectAtIndex:1] childAtIndex:0] stringValue];
    tempSupplier.address = [[(GDataXMLElement*)[children objectAtIndex:2] childAtIndex:0] stringValue];
    tempSupplier.road = [[(GDataXMLElement*)[children objectAtIndex:3] childAtIndex:0] stringValue];
    tempSupplier.mapAddress = [[(GDataXMLElement*)[children objectAtIndex:4] childAtIndex:0] stringValue];
    tempSupplier.longitude = [[[(GDataXMLElement*)[children objectAtIndex:5] childAtIndex:0] stringValue] floatValue];
    tempSupplier.latitude = [[[(GDataXMLElement*)[children objectAtIndex:6] childAtIndex:0] stringValue] floatValue];
    tempSupplier.phone = [[(GDataXMLElement*)[children objectAtIndex:7] childAtIndex:0] stringValue];
    tempSupplier.phone2 = [[(GDataXMLElement*)[children objectAtIndex:8] childAtIndex:0] stringValue];
    tempSupplier.telephone = [[(GDataXMLElement*)[children objectAtIndex:9] childAtIndex:0] stringValue];
    tempSupplier.telephone2 = [[(GDataXMLElement*)[children objectAtIndex:10] childAtIndex:0] stringValue];
    tempSupplier.joinTime = [[(GDataXMLElement*)[children objectAtIndex:11] childAtIndex:0] stringValue];
    tempSupplier.startTime = [[[(GDataXMLElement*)[children objectAtIndex:12] childAtIndex:0] stringValue] integerValue];
    tempSupplier.endTime = [[[(GDataXMLElement*)[children objectAtIndex:13] childAtIndex:0] stringValue] integerValue];
    tempSupplier.leastPrice = [[[(GDataXMLElement*)[children objectAtIndex:14] childAtIndex:0] stringValue] floatValue];
    tempSupplier.leastDistance = [[[(GDataXMLElement*)[children objectAtIndex:15] childAtIndex:0] stringValue] floatValue];
    tempSupplier.deliveryDesc = [[(GDataXMLElement*)[children objectAtIndex:16] childAtIndex:0] stringValue];
    tempSupplier.avgPrice =  [[[(GDataXMLElement*)[children objectAtIndex:17] childAtIndex:0] stringValue] floatValue];
    tempSupplier.consumerecord = [[[(GDataXMLElement*)[children objectAtIndex:18] childAtIndex:0] stringValue] integerValue];
    tempSupplier.imgUrl = [[(GDataXMLElement*)[children objectAtIndex:19] childAtIndex:0] stringValue];
    tempSupplier.desc = [[(GDataXMLElement*)[children objectAtIndex:20] childAtIndex:0] stringValue];
    tempSupplier.supCateId = [[[(GDataXMLElement*)[children objectAtIndex:21] childAtIndex:0] stringValue] integerValue];
    tempSupplier.city = [[[(GDataXMLElement*)[children objectAtIndex:22] childAtIndex:0] stringValue] integerValue];
    tempSupplier.modifyDate = [[(GDataXMLElement*)[children objectAtIndex:23] childAtIndex:0] stringValue];
    tempSupplier.supplierCategoryName = [[(GDataXMLElement*)[children objectAtIndex:24] childAtIndex:0] stringValue];    
    
    return tempSupplier;
}

-(NSArray *) getSupplierByUrl:(NSString *)URIString{
    __strong NSURL *url2 = [[NSURL alloc] initWithString:[URIString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"url2%@",url2);
    __strong NSMutableURLRequest* request = [NSMutableURLRequest new];    
    [request setURL:url2];    
    [request setHTTPMethod:@"GET"]; 
    __strong NSHTTPURLResponse* response;    
    __strong NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
    __strong NSString* responseXMLResult=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSError *error;
    __strong GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult options:0 error:&error];
    
    __strong NSArray *children = [doc.rootElement children]; 
//    result = [NSMutableArray arrayWithObject:supplier];
    result=[[NSMutableArray alloc]init];
    for (GDataXMLElement *element in children)
    {
        if ([element isKindOfClass:[GDataXMLElement class]]) 
        {
            __strong Supplier *supplier=[[Supplier alloc] init];
            supplier.supplierId=[[[element childAtIndex:0] stringValue] intValue];
            supplier.supplierName=[[element childAtIndex:1] stringValue];
            supplier.address=[[element childAtIndex:2] stringValue];
            supplier.road=[[element childAtIndex:3] stringValue];
            supplier.mapAddress=[[element childAtIndex:4] stringValue];
            supplier.longitude=[[[element childAtIndex:5] stringValue] floatValue];
            supplier.latitude=[[[element childAtIndex:6] stringValue] floatValue];
            supplier.phone=[[element childAtIndex:7] stringValue];
            supplier.phone2=[[element childAtIndex:8] stringValue];
            supplier.telephone=[[element childAtIndex:9] stringValue];
            supplier.telephone2=[[element childAtIndex:10] stringValue];
            supplier.joinTime=[[element childAtIndex:11] stringValue];
            supplier.startTime=[[[element childAtIndex:12] stringValue] intValue];
            supplier.endTime=[[[element childAtIndex:13] stringValue] intValue];
            supplier.leastPrice=[[[element childAtIndex:14] stringValue] floatValue];
            supplier.leastDistance=[[[element childAtIndex:15] stringValue] floatValue];
            supplier.deliveryDesc=[[element childAtIndex:16] stringValue];
            supplier.avgPrice=[[[element childAtIndex:17] stringValue] floatValue];
            supplier.consumerecord=[[[element childAtIndex:18] stringValue] intValue];
            supplier.imgUrl=[[element childAtIndex:19] stringValue];
            supplier.desc=[[element childAtIndex:20] stringValue];
            supplier.supCateId=[[[element childAtIndex:21] stringValue] intValue];
            supplier.city=[[[element childAtIndex:22] stringValue] intValue];
            supplier.modifyDate = [[element childAtIndex:23] stringValue];
            supplier.supplierCategoryName = [[element childAtIndex:24] stringValue];
            //supplier.status=nil;
//            if (result == nil)
//            {
//                result = [NSMutableArray arrayWithObject:supplier];
//            }
//            else
//            {
                [result addObject:supplier];
//            }
        }
    } 
    NSLog(@"result%d",[result count]);
    return result;
}

-(NSArray *) getSupplier:(NSString *)supplierCategoryId pageIndex:(NSInteger) pageIndex road:(NSString *) road isSelling:(NSString *) isSelling{
    __strong NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_SUPPLIERS_BY_CATEGORIES,@"?"]];
    
    URIString = [URIString stringByAppendingFormat:@"%@&%@",[NSString stringWithFormat: PAGEINDEX,pageIndex],PAGESIZE];
    if(supplierCategoryId != nil){
        URIString = [URIString stringByAppendingFormat:@"&%@",[NSString stringWithFormat: @"supCateId=%@",supplierCategoryId]];
    }
    if(road != nil){
        URIString = [URIString stringByAppendingFormat:@"&%@",[NSString stringWithFormat: ROAD,road]]; 
    }
    if(isSelling != nil){
        URIString = [URIString stringByAppendingFormat:@"&%@",[NSString stringWithFormat: ISSELLING,isSelling]]; 
    }
    NSLog(@"uristring%@",URIString);
    return [self getSupplierByUrl:URIString];
}

-(NSArray *) search:(NSString *)searchText pageIndex:(NSInteger) pageIndex {

    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_SEARCH,searchText]];
    if (pageIndex > 0) {
        URIString = [URIString stringByAppendingFormat:@"&%@&%@",[NSString stringWithFormat: PAGEINDEX,pageIndex],PAGESIZE]; 
    }
    return [self getSupplierByUrl:URIString];
}

//-(NSString *) getNumCount:(NSInteger) supId{
//
//    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat:@"consume?supId=%d",supId]];
//
//    
//    
//    
//    NSURL *url2 = [[NSURL alloc] initWithString:[URIString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//    NSMutableURLRequest* request = [NSMutableURLRequest new];    
//    [request setURL:url2];    
//    [request setHTTPMethod:@"GET"]; 
//    NSHTTPURLResponse* response;    
//    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
//    NSString* responseXMLResult=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    NSError *error;
//    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult options:0 error:&error];
//    [url2 release];
//    [request release];
//    [responseXMLResult release];
//    
//    
//    
//
//    NSArray *children = [doc.rootElement children]; 
//    NSString *result = [(GDataXMLElement*)[children objectAtIndex:0] stringValue];
//    NSString *countNum = [(GDataXMLElement*)[children objectAtIndex:1] stringValue];
//    if ([result isEqualToString:SUCCESS_MSG]) {
//        return countNum;
//    }
//    [doc release];
//    return @"fail";
//}

@end
